
module scene {

    export class AddStoneScene extends eui.Component {
        constructor() {
            super();
            this.addEventListener(eui.UIEvent.COMPLETE, this.onComplete, this);
            this.skinName = "AddStonePageSkin";
        }

        private addStoneBtn: eui.Image;
        private addStoneRejectBtn: eui.Label;

        private onComplete() {
            manager.StatisticsManager.sceneCount(1009)
            console.log('AddStoneScene.onComplete')
            this.addStoneBtn.addEventListener(egret.TouchEvent.TOUCH_TAP, this.addStone, this)
            this.addStoneRejectBtn.addEventListener(egret.TouchEvent.TOUCH_TAP, this.addStoneRcject, this)
        }

        private addStone() {
            let adManager = manager.GameManager.getInstance().getAdManager();
            let revivalMethod = Number(adManager.ad_percent) - Math.random() * 100
            if(revivalMethod >= 0){
                adManager.showVideoAd();
                adManager.addEventListener(manager.AdManager.VIDEO_AD_CLOSE,
                    this.onVideoAdClose, this);
            }else{

                manager.ShareManager.getInstance().addEventListener(manager.ShareManager.SHARE_STATUS,
                    this.onShareStatus, this);
                manager.GameManager.getInstance().commonShare();
            }
        }

        private onShareStatus(eve: egret.Event): void {
            manager.ShareManager.getInstance().removeEventListener(manager.ShareManager.SHARE_STATUS,
                this.onShareStatus, this);
            let succ: number = eve.data;
            if (succ > 0) {
                this.onShareSucc();
            }
        }

        private addStoneRcject() {
            let event: events.AddStoneRejectEvent = new events.AddStoneRejectEvent();
            this.dispatchEvent(event)
        }

        private onVideoAdClose(eve: egret.Event): void {
            egret.log("视频广告播放事件:", eve.data);
            if (eve.data != null && eve.data.isEnded) {
                manager.GameManager.getInstance().adManager.removeEventListener(manager.AdManager.VIDEO_AD_CLOSE,
                    this.onVideoAdClose, this);
                this.onShareSucc();
            }
        }

        private onShareSucc(): void {
            let event: events.AddStoneEvent = new events.AddStoneEvent();
            this.dispatchEvent(event)
        }
    }
}